/*****************************************************************************
 *  CommLayerLib for Windowed Systems - TCPServerLogger.java
 *  Copyright (C) 2012  Matteo Piscitelli
 *  E-mail: matteo@picciux.it
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  For further info, check http://code.google.com/p/java-comm-layer/
 *
 *  SVN: $Id: TCPServerLogger.java 48 2012-10-11 18:55:39Z picciux@gmail.com $
 *  
 *****************************************************************************/

package it.picciux.commlayer.win.log;

import it.picciux.commlayer.win.net.NetworkDataBroadcaster;

import java.io.IOException;
import java.io.OutputStream;

public class TCPServerLogger extends StreamLogger {
	//TODO manage termination!!!
	private NetworkDataBroadcaster server;
	
	public TCPServerLogger(String moduleName, int port) throws IOException {
		super(moduleName);
		server = new NetworkDataBroadcaster(port);
		
		OutputStream os = new OutputStream() {
			@Override
			public void write(int b) throws IOException {
				server.writeData(b);
			}

			@Override
			public void write(byte[] b) throws IOException {
				server.writeData(b);
			}

			@Override
			public void write(byte[] b, int off, int len) throws IOException {
				server.writeData(b, off, len);
			}
		};
		
		setOutputStream(os);
		
		if (! server.openLog())
			throw new IOException("Impossible to listen on port " + port);
	}

}
